# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId


class ZoneInfo(db.Model, JsonEncoder):
    __tablename__ = "zones"
    zone_id = db.Column(db.String, primary_key = True)
    env_name = db.Column(db.String)
    zone_code = db.Column(db.String)
    zone_alias = db.Column(db.String)
    zone_title = db.Column(db.String)
    parent_code = db.Column(db.String)
    barrier_url = db.Column(db.String)
    current_status = db.Column(db.String)
    operate_group = db.Column(db.String)
    expect_status = db.Column(db.String)
    update_time = db.Column(db.DATETIME)
    check_status = db.Column(db.String)

    def __init__(self, env_name = None, zone_code = None, zone_alias = None, zone_title = None, parent_code = None,
                 barrier_url = None, current_status = None, operate_group = None, update_time = None,
                 expect_status = None, check_status = None):
        self.zone_id = createId('zo')
        self.env_name = env_name
        self.zone_code = zone_code
        self.zone_alias = zone_alias
        self.zone_title = zone_title
        self.parent_code = parent_code
        self.barrier_url = barrier_url
        self.current_status = current_status
        self.operate_group = operate_group
        self.update_time = update_time
        self.expect_status = expect_status
        self.check_status = check_status

    def to_dict(self):
        return {
            'zone_id': self.zone_id,
            'env_name': self.env_name,
            'zone_code': self.zone_code,
            'zone_alias': self.zone_alias,
            'zone_title': self.zone_title,
            'parent_code': self.parent_code,
            'barrier_url': self.barrier_url,
            'current_status': self.current_status,
            'operate_group': self.operate_group,
            'expect_status': self.expect_status,
            'check_status': self.check_status,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
        }
